﻿// The project released under MS-PL license https://daxeditor.codeplex.com/license

using System;
using System.Linq;
using Babel.Parser;

namespace DaxEditor
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable")]
    public class DaxParsingException : Exception
    {
        public DaxParsingException() { }

        public DaxParsingException(string message) : base(message) { }

        public DaxParsingException(string message, Exception inner) : base(message, inner) { }

        static public DaxParsingException FromHandler(Babel.Parser.ErrorHandler handler)
        {
            var sortedErrors = handler.SortedErrorList();
            var messages = string.Join("\r\n", sortedErrors.Take(Babel.Parser.Parser.MaxErrors).Select<Error, string>(i => i.ToString()));
            if (sortedErrors.Count > Babel.Parser.Parser.MaxErrors)
                messages = string.Format("First {0} errors:\r\n{1}", Babel.Parser.Parser.MaxErrors, messages);

            return new DaxParsingException(messages);
        }
    }

}
